package com.dimong.www.loan.utils;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;

public class ViewUtils {

    /**
     * 计算view大小
     *
     * @param view
     * @return
     */
    public static int[] unDisplayViewSize(View view) {
        if (view == null) {
            return new int[]{0, 0};
        }
        int[] size = new int[2];
        int width = View.MeasureSpec.makeMeasureSpec(0,
            View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0,
            View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        size[0] = view.getMeasuredWidth();
        size[1] = view.getMeasuredHeight();
        return size;
    }

    private static final Rect viewRect = new Rect();

    /**
     * 计算View当前可视百分比
     */
    public static int getViewVisibilityPercents(View currentView) {
        if (currentView == null) {
            return 0;
        }
        int percents = 100;

        currentView.getLocalVisibleRect(viewRect);
        int height = currentView.getHeight();

        if (height <= 0) {
            return 0;
        }
        if (viewIsPartiallyHiddenTop()) {
            percents = (height - viewRect.top) * 100 / height;
        } else if (viewIsPartiallyHiddenBottom(height)) {
            percents = viewRect.bottom * 100 / height;
        } else if (viewRect.bottom <= 0 || viewRect.top >= height) {
            percents = 0;
        }
        return percents;
    }

    private static boolean viewIsPartiallyHiddenBottom(int height) {
        return viewRect.bottom > 0 && viewRect.bottom < height;
    }

    private static boolean viewIsPartiallyHiddenTop() {
        return viewRect.top > 0;
    }

    /**
     * 设置ViewGroup中子view的大小位置
     */
    public static void setViewParamsMargin(View v, int left, int right, int top, int bottom) {
        if (v == null) {
            return;
        }
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        if (params == null) {
            return;
        }
        int left2 = params.leftMargin;
        int right2 = params.rightMargin;
        int top2 = params.topMargin;
        int bottom2 = params.bottomMargin;
        params.setMargins(left != 0 ? left : left2, top != 0 ? top : top2, right != 0 ? right : right2, bottom != 0 ? bottom : bottom2);    //控件相对父控件左上右下的距离
        v.setLayoutParams(params);
        v.requestLayout();
    }

    /**
     * 设置ViewGroup中子view的大小位置
     */
    public static void setViewParams(View v, int width, int height, int left, int top) {
        if (v == null) {
            return;
        }
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        if (params == null) {
            return;
        }
        params.setMargins(left, top, 0, 0);    //控件相对父控件左上右下的距离
        if (width >= 0)
            params.width = width;
        if (height >= 0)
            params.height = height;
        v.setLayoutParams(params);
        v.requestLayout();
    }


    public static void setViewParams(View v, int width, int height) {
        if (v == null) {
            return;
        }
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        if (params == null) {
            return;
        }
        if (width >= 0)
            params.width = width;
        if (height >= 0)
            params.height = height;
        v.setLayoutParams(params);
    }

    /**
     * 设置ViewGroup中子view的大小位置
     */

    public static void setViewParams(View v, int width, int height, int left, int top, int right, int bottom) {
        if (v == null) {
            return;
        }
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        if (params == null) {
            return;
        }
        params.setMargins(left, top, right, bottom);    //控件相对父控件左上右下的距离
        if (width >= 0)
            params.width = width;
        if (height >= 0)
            params.height = height;
        v.setLayoutParams(params);
        v.requestLayout();
    }
}
